package View;

import javax.swing.*;


import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import com.toedter.calendar.JDateChooser;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import Model.*;
import Controller.*;

public class TelaMeta extends JPanel{
	GerenciadorFinancas gf = new GerenciadorFinancas();
	Meta me = new Meta();
	JTabbedPane meta;
	JPanel m;
	JScrollPane scrollPane;
	JComboBox tipo;
	JDateChooser inicio, fim;
	JSpinner pa, age;
	JFormattedTextField lucro;
	
	public TelaMeta() {
		setLayout(new GridLayout(0, 1, 0, 0));
		meta = new JTabbedPane(JTabbedPane.BOTTOM);
		m = new JPanel();
		meta.addTab("META", null, m, null);
		m.setBackground(null);
		
		scrollPane = new JScrollPane();
		scrollPane.getViewport().setBackground(new Color(100, 175, 239));
		
		GroupLayout gl_m = new GroupLayout(m);
		gl_m.setHorizontalGroup(
			gl_m.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_m.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane)
					.addContainerGap())
		);
		gl_m.setVerticalGroup(
			gl_m.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_m.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane)
					.addGap(31))
		);
		
		JPanel panel = new JPanel();
		panel.setBackground(null);
		scrollPane.setViewportView(panel);
		
		JLabel lblIniciarMetaEm = new JLabel("INICIAR EM");
		lblIniciarMetaEm.setBounds(10, 71, 136, 14);
		lblIniciarMetaEm.setHorizontalAlignment(SwingConstants.RIGHT);
		
		inicio = new JDateChooser();
		inicio.setBounds(154, 65, 87, 20);
		
		JLabel lblFinalizarEm = new JLabel("FINALIZAR EM");
		lblFinalizarEm.setHorizontalAlignment(SwingConstants.RIGHT);
		lblFinalizarEm.setBounds(10, 99, 136, 14);
		
		fim = new JDateChooser();
		fim.setBounds(156, 93, 87, 20);
		
		JLabel lblAgendamentos = new JLabel("AGENDAMENTOS");
		lblAgendamentos.setBounds(10, 158, 136, 14);
		lblAgendamentos.setHorizontalAlignment(SwingConstants.RIGHT);
		
		 age = new JSpinner();
		age.setBounds(154, 155, 87, 20);
		
		JLabel lblPacientes_1 = new JLabel("PACIENTES");
		lblPacientes_1.setHorizontalAlignment(SwingConstants.RIGHT);
		lblPacientes_1.setBounds(10, 130, 136, 14);
		
		 pa = new JSpinner();
		pa.setBounds(156, 127, 87, 20);
		
		JLabel lblLucroMnimo = new JLabel("LUCRO M\u00CDNIMO");
		lblLucroMnimo.setHorizontalAlignment(SwingConstants.RIGHT);
		lblLucroMnimo.setBounds(10, 189, 136, 14);
		
		 lucro = new JFormattedTextField();
		lucro.setBounds(154, 186, 87, 20);
		
		JLabel lblEstadoDaMeta_1 = new JLabel("META EM ANDAMENTO");
		lblEstadoDaMeta_1.setBounds(334, 71, 136, 14);
		lblEstadoDaMeta_1.setForeground(Color.RED);
		
		JLabel lblLegenda = new JLabel("LEGENDA:");
		lblLegenda.setBounds(334, 46, 136, 14);
		
		JLabel lblMetaAlcanada = new JLabel("META ALCAN\u00C7ADA");
		lblMetaAlcanada.setBounds(334, 99, 136, 14);
		lblMetaAlcanada.setForeground(Color.GREEN);
		panel.setLayout(null);
		panel.add(lblIniciarMetaEm);
		panel.add(lblPacientes_1);
		panel.add(lblAgendamentos);
		panel.add(lblLucroMnimo);
		panel.add(inicio);
		panel.add(age);
		panel.add(lblFinalizarEm);
		panel.add(fim);
		panel.add(pa);
		panel.add(lucro);
		panel.add(lblLegenda);
		panel.add(lblEstadoDaMeta_1);
		panel.add(lblMetaAlcanada);
		
		JButton button = new JButton("OK");
		button.setBackground(new Color(255, 255, 255));
		button.setForeground(new Color(0, 175, 239));

		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				editar();
			}
		});
		button.setBounds(175, 217, 65, 32);
		panel.add(button);
		
		tipo = new JComboBox();
		tipo.setModel(new DefaultComboBoxModel(new String[] {"DIARIA", "NORMAL"}));
		tipo.setBounds(154, 37, 87, 20);
		panel.add(tipo);
		
		JLabel lblTipo = new JLabel("TIPO");
		lblTipo.setHorizontalAlignment(SwingConstants.RIGHT);
		lblTipo.setBounds(10, 40, 136, 14);
		panel.add(lblTipo);
		m.setLayout(gl_m);
		add(meta);

	}
	
	public Meta getMeta(){
		me.tipo = tipo.getSelectedItem().toString();
		me.inicio = inicio.getCalendar();
		me.fim = fim.getCalendar();
		me.pacientes = Integer.parseInt(pa.getValue().toString());
		me.agendamentos = Integer.parseInt(age.getValue().toString());
		me.lucro_m = Integer.parseInt(lucro.getText());
		return me;
	}
	
	public void editar(){
		gf.editarMeta(getMeta());
		TelaPrincipal.AbrirFinancas();
	}
	
}